package networks.manager;

import java.util.HashMap;
import networks.event.Delegate;
import networks.protocol.Message;

public class CallbackFactory {
    private static final CallbackFactory _instance = new CallbackFactory();

    private CallbackFactory() {
        _callbacks = new HashMap<String, Delegate.ActionOne<Message>>();
    }

    public synchronized static CallbackFactory Instance() {
        return _instance;
    }

    public void Dispose() {
        _callbacks.clear();
    }

    private HashMap<String, Delegate.ActionOne<Message>> _callbacks;

    public HashMap<String, Delegate.ActionOne<Message>> getCallbacks() {
        return _callbacks;
    }

    public void RegistCallbacks() {
        // registCallback(Routes.C2S_GATE_CONNECT, GameHandler.onResConnect);
        // connector
        _callbacks.put(Routes.C2S_CONNECTOR_LOGIN, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResLogin(msg);
            }
        });
        _callbacks.put(Routes.C2S_CONNECTOR_LOGOUT, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResLogout(msg);
            }
        });
        _callbacks.put(Routes.C2S_CONNECTOR_REGISTER, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResRegister(msg);
            }
        });
        // lobby
        _callbacks.put(Routes.C2S_LOBBY_MATCH, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResMatch(msg);
            }
        });
        // registCallback(Routes.C2S_LOBBY_UNMATCH, GameHandler.onResUnmatch);
        // registCallback(Routes.C2S_LOBBY_SEND_CHAT, GameHandler.onResSendChat);
        // room
        _callbacks.put(Routes.C2S_ROOM_CREATE, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResRoomCreate(msg);
            }
        });
        _callbacks.put(Routes.C2S_ROOM_JOIN, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResRoomJoin(msg);
            }
        });
        _callbacks.put(Routes.C2S_ROOM_LEAVE, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResRoomLeave(msg);
            }
        });
        // registCallback(Routes.C2S_ROOM_PLAYER_ACTION, GameHandler.onResPlayerAction);
        // database
        _callbacks.put(Routes.C2S_DB_GET_USER, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResGetUser(msg);
            }
        });
        _callbacks.put(Routes.C2S_DB_RENAME, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResRename(msg);
            }
        });
        _callbacks.put(Routes.C2S_DB_GET_DOUDIZHU_INFO, new Delegate.ActionOne<Message>() {
            @Override
            public void Invoke(Message msg) {
                GameHandler.onResGetDoudizhuInfo(msg);
            }
        });
    }

    private void registCallback(String route, Delegate.ActionOne<Message> handler) {
        if (_callbacks.containsKey(route)) {
            // 已注册过同名的
            System.out.println("already exist route: " + route);
            return;
        }
        _callbacks.put(route, handler);
    }

    public void InvokeHandle(String route, Message data) {
        Delegate.ActionOne<Message> handler = _callbacks.get(route);
        if (handler != null) {
            handler.Invoke(data);
        } else {
            System.out.println("cannot find handler for " + route);
        }
    }
}
